//
//  ViewController.m
//  book
//
//  Created by Fenhua on 16/12/6.
//  Copyright © 2016年 huangfeng. All rights reserved.
//

#import "ViewController.h"
#import "WenjianViewController.h"
#import "TextViewController.h"
#import "View2Controller.h"
@interface ViewController ()
{
    NSArray<NSURL *>*image;
    NSArray<NSURL *>*texts;
}
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    self.title = @"我的书架";
    NSBundle *bun = [NSBundle mainBundle];
    image = [bun URLsForResourcesWithExtension:@"jpeg" subdirectory:nil];
    texts = [bun URLsForResourcesWithExtension:@"txt" subdirectory:nil];

}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (!image || image.count == 0) {
        return 0;
    }
    NSInteger cunt = image.count - 1;
    return cunt/3 + 1;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    UITableViewCell *cell = [[UITableViewCell alloc]init];
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    cell.backgroundView = [[UIImageView alloc]initWithImage:[UIImage imageNamed:@"BookShelfCell"]];
    NSInteger index = indexPath.row;
    NSInteger indexs = index*3;
    for (int i = 0; i<3 && indexs+i <image.count ; i++) {
        int space = 15;
        int width = (self.view.frame.size.width - space*4)/3;
        UIImage *img = [UIImage imageNamed:image[indexs+i].lastPathComponent];
        UIImageView *iv = [[UIImageView alloc]initWithFrame:CGRectMake((space + width)*i +space , 8, width, 139-8-8*2)];
        iv.image = img;
        [cell addSubview:iv];
        iv.tag = 100+indexs+i;
        iv.userInteractionEnabled = YES;
        UITapGestureRecognizer *recog = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(onTap:)];
        recog.numberOfTapsRequired = 1;
        recog.numberOfTouchesRequired = 1;
        [iv addGestureRecognizer:recog];
        
    }
    return cell;
}

- (void)onTap:(UITapGestureRecognizer *)tap
{
    UIImageView *iv = (UIImageView *)tap.view;
    NSInteger index = iv.tag-100;
    NSURL *imgURL = image[index];
    
    //获取小说名
    NSString *fileName = imgURL.lastPathComponent;
    fileName = fileName.stringByDeletingPathExtension;
    //NSLog(@"fileName:%@",fileName);
    
    NSURL *tURL;
    for (NSURL *url in texts) {
        if ([url.absoluteString rangeOfString:fileName].location != NSNotFound) {
            //把小说地址赋值给tURL
            tURL = url;
            break;
        }
    }
    
    NSError *err;
    
    //获取小说内容
    NSString *content = [[NSString alloc] initWithContentsOfURL:tURL encoding:NSUTF8StringEncoding error:&err];
    if (err) {
        NSLog(@"%@",err.description);
    }
    NSLog(@"content:%@",content);
//    WenjianViewController *con = [[WenjianViewController alloc]init];
//    con.hidesBottomBarWhenPushed = NO;
//    [self.navigationController pushViewController:con animated:YES];
    //TextViewController *con = [[TextViewController alloc]init];
    UIViewController *con = [self.storyboard instantiateViewControllerWithIdentifier:@"view"];
    [self.navigationController pushViewController:con animated:YES];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 139;
}



- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
    
}

@end
